package com.swjd.filter;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

//同步发送消息
public class Producer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //消息发送者（生产着)
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //发送给服务器地址
        producer.setNamesrvAddr("localhost:9876");
        //启动服务
        producer.start();
        //创建要发送的消息·
        for (int i = 0;i<10; i++){
            String msg = ("这是第"+i+"条消息");
            Message message = new Message("topic4", "tag1", msg.getBytes());
            //消息追加属性
            message.putUserProperty("name","zhangsan");
            message.putUserProperty("age","15");
            //发送消息
            SendResult sendResult = producer.send(message);
            //输出发送结果
            System.out.println(sendResult);
        }
        //关闭
        producer.shutdown();
    }
}
